package com.xintai.sparepart.spareandpart.controller;

import java.util.List;

import com.xintai.common.constant.Constants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xintai.common.annotation.Log;
import com.xintai.common.core.controller.BaseController;
import com.xintai.common.core.domain.AjaxResult;
import com.xintai.common.enums.BusinessType;
import com.xintai.sparepart.spareandpart.domain.BeiKuwei;
import com.xintai.sparepart.spareandpart.service.IBeiKuweiService;
import com.xintai.common.utils.poi.ExcelUtil;
import com.xintai.common.core.page.TableDataInfo;

/**
 * 库位Controller
 *
 * @author xintai
 * @date 2020-12-23
 */
@RestController
@RequestMapping("/sparepart/kuwei")
public class BeiKuweiController extends BaseController
{
    @Autowired
    private IBeiKuweiService beiKuweiService;

    /**
     * 查询库位列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:list')")
    @GetMapping("/list")
    public TableDataInfo list(BeiKuwei beiKuwei)
    {
        startPage();
        beiKuwei.setKuweiIsdel(Constants.SUCCESS);
        List<BeiKuwei> list = beiKuweiService.selectBeiKuweiList(beiKuwei);
        return getDataTable(list);
    }

    /**
     * 导出库位列表
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:export')")
    @Log(title = "库位", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BeiKuwei beiKuwei)
    {
        List<BeiKuwei> list = beiKuweiService.selectBeiKuweiList(beiKuwei);
        ExcelUtil<BeiKuwei> util = new ExcelUtil<BeiKuwei>(BeiKuwei.class);
        return util.exportExcel(list, "kuwei");
    }

    /**
     * 获取库位详细信息
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:query')")
    @GetMapping(value = "/{kuweiId}")
    public AjaxResult getInfo(@PathVariable("kuweiId") Long kuweiId)
    {
        return AjaxResult.success(beiKuweiService.selectBeiKuweiById(kuweiId));
    }

    /**
     * 新增库位
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:add')")
    @Log(title = "库位", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BeiKuwei beiKuwei)
    {
        if (beiKuweiService.selectkuweiNamesake(beiKuwei.getCangkuId(),beiKuwei.getKuweiName())){
            return AjaxResult.error("该仓库以存在:'"+beiKuwei.getKuweiName()+"'库位");
        }
        return toAjax(beiKuweiService.insertBeiKuwei(beiKuwei));
    }

    /**
     * 修改库位
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:edit')")
    @Log(title = "库位", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BeiKuwei beiKuwei)
    {
        return toAjax(beiKuweiService.updateBeiKuwei(beiKuwei));
    }

    /**
     * 删除库位
     */
    @PreAuthorize("@ss.hasPermi('sparepart:kuwei:remove')")
    @Log(title = "库位", businessType = BusinessType.DELETE)
	@DeleteMapping("/{kuweiIds}")
    public AjaxResult remove(@PathVariable Long[] kuweiIds)
    {
        return toAjax(beiKuweiService.deleteBeiKuweiByIds(kuweiIds));
    }
}
